import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class Room
{
   private int capacity; // capacity of this room
   private DailySchedule weeklySchedule[]; // weekly schedule of this room

   public Room(int capacity)
   {
      this.capacity = capacity;
      weeklySchedule = new DailySchedule[Calendar.DAY_OF_WEEK];
      
   }
   public void add(Request r)
   {
      if(r.getDay().toLowerCase().contains("m"))
      {
         if(weeklySchedule[1] == null)
         weeklySchedule[1] = new DailySchedule();
         
         weeklySchedule[1].add(r);
      }
      if(r.getDay().toLowerCase().contains("tu"))
      {
         if(weeklySchedule[2] == null)
         weeklySchedule[2] = new DailySchedule();
         weeklySchedule[2].add(r);
      }
      if(r.getDay().toLowerCase().contains("w"))
      {
         if(weeklySchedule[3] == null)
         weeklySchedule[3] = new DailySchedule();
         weeklySchedule[3].add(r);
      }
      if(r.getDay().toLowerCase().contains("th"))
      {
         if(weeklySchedule[4] == null)
         weeklySchedule[4] = new DailySchedule();
         weeklySchedule[4].add(r);
      }
      if(r.getDay().toLowerCase().contains("f"))
      {
         if(weeklySchedule[5] == null)
         weeklySchedule[5] = new DailySchedule();
         weeklySchedule[5].add(r);
      }
      if(r.getDay().toLowerCase().contains("s"))
      {
         if(weeklySchedule[6] == null)
         weeklySchedule[6] = new DailySchedule();
         weeklySchedule[6].add(r);
      }
      if(r.getDay().toLowerCase().contains("su"))
      {
         if(weeklySchedule[0] == null)
         weeklySchedule[0] = new DailySchedule();
         weeklySchedule[0].add(r);
      }
      
   }
   public void cancel(Request r)
   {
      for(int i = 0;i<weeklySchedule.length;i++)
      {
         if(weeklySchedule[i] == null)
         {}
         else
         weeklySchedule[i].cancel(r);
      }
   }
   public boolean checkAvailability(Request r)
   {
      boolean result = false;
      for(int i = 0;i<weeklySchedule.length && !result;i++)
      {
           result = weeklySchedule[i].check(r);
      }
      return result;
   }
   public void fileOutput(PrintWriter writer, Date date) throws FileNotFoundException
   {
      writer = new PrintWriter("output.txt");
      Calendar calendar = new GregorianCalendar();
      calendar.setTime(date) ; 
      SimpleDateFormat formatter = new SimpleDateFormat("E MM/dd/yy") ; 
      String formattedDate = formatter.format(date);
      writer.println(formattedDate) ;  // prints the formatted date
      writer.println(this.toString());
      
      writer.close();
   }
   public int getCapacity()
   {
      return capacity;
   }
   
   public String toString()
   {
      String result = "";
      String week[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
   
      for(int i=0;i<weeklySchedule.length;i++)
      {
         if(weeklySchedule[i] == null)
         {}
         else
         {
            result = result + week[i] + "\n" + weeklySchedule[i];
         }
      }
      
      return result;
   }
}
